package com.xxx.flink.sink;

import com.xxx.flink.pojo.Event;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * Sink到kafka
 */
public class SinkToKafka {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 从kafka中读取数据
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");
        DataStreamSource<String> kafkaStream = env.addSource(new FlinkKafkaConsumer<>("click", new SimpleStringSchema(StandardCharsets.UTF_8), props));
        kafkaStream.print();

        // 使用flink将从kafka读取到的内容进行map转换
        SingleOutputStreamOperator<String> result = kafkaStream.map(new MapFunction<String, String>() {
            @Override
            public String map(String s) throws Exception {
                String[] arr = s.split(",");
                return new Event(arr[0], arr[1], Long.valueOf(arr[2])).toString();
            }
        });

        // sink到kafka
        result.addSink(new FlinkKafkaProducer<>("localhost:9092", "event", new SimpleStringSchema()));

        env.execute("kafka");
    }

}
